package com.example.dao;

import com.example.domain.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {

    Integer addUser(User user);

    Integer editUser(User user);



    @Select("select * from user where user_email = #{email} and user_password = #{password} and user_status = '0' ")
    @Results(
            id = "UserMap",
            value = {
                    @Result(id = true, column = "user_id", property = "id"),
                    @Result(column = "user_name", property = "name"),
                    @Result(column = "user_password", property = "password"),
                    @Result(column = "user_email", property = "email"),
                    @Result(column = "user_role", property = "role"),
                    @Result(column = "user_status", property = "status"),
            }
    )
    User findUserByEmailAndPassword(User user);

    @Select("      <script>\n" +
            "            select * from user\n" +
            "            <where>\n" +
            "                <if test=\"userId!=null \">\n" +
            "                    and user_id like concat('%',#{userId},'%')\n" +
            "                </if>\n" +
            "            </where>\n" +
            "        </script>")
    @ResultMap("UserMap")
    Page<User> findAllUsers(Integer userId);

    @Select("      <script>\n" +
            "            select * from user\n" +
            "            <where>\n" +
            "                <if test=\"name!=null and name.trim()!='' \">\n" +
            "                    and user_name like concat('%',#{name},'%')\n" +
            "                </if>\n" +
            "                <if test=\"id!=null \">\n" +
            "                    and user_id like concat('%',#{id},'%')\n" +
            "                </if>\n" +
            "                <if test=\"email!=null and email.trim()!='' \">\n" +
            "                    and user_email like concat('%',#{email},'%')\n" +
            "                </if>\n" +
            "            </where>\n" +
            "        </script>")
    @ResultMap("UserMap")
    Page<User> searchUsers(User user);

    @Select("select * from user where user_id = #{id}")
    @ResultMap("UserMap")
    User findUserById(String id);

}
